/*
 * @Author: early-autumn
 * @Date: 2020-05-06 15:16:13
 * @LastEditors: early-autumn
 * @LastEditTime: 2020-05-30 23:19:14
 */
import dayjs from 'dayjs'

/***
 * 获得扎帐的时间
 */
export function getDebitTime(date) {
  const day = dayjs(date)
  return [
    day.startOf('day').subtract(1, 'day').hour(16).minute(0).second(0),
    day.endOf('day').hour(16).minute(0).second(0),
  ]
}

/***
 * 获得今日起止时间
 */
export function getTodayStartWithEnd(date) {
  const day = dayjs(date)
  return [day.startOf('day'), day.endOf('day')]
}

/***
 * 获得昨日起止时间
 */
export function getYesterdayStartWithEnd(date) {
  const day = dayjs(date).subtract(1, 'day')
  return [day.startOf('day'), day.endOf('day')]
}

/**
 * 获取明日起止日期
 */
export function getTomorrowStartWithEnd(date) {
  const day = dayjs(date).add(1, 'day')
  return [day.startOf('day'), day.endOf('day')]
}

/**
 * 获取本周起止日期
 */
export function getThisWeekStartWithEnd(date) {
  const week = dayjs(date)
  return [week.startOf('week').add(1, 'day'), week.endOf('week').add(1, 'day')]
}

/**
 * 获取上周起止日期
 */
export function getLastWeekStartWithEnd(date) {
  const week = dayjs(date).subtract(1, 'week')
  return [week.startOf('week').add(1, 'day'), week.endOf('week').add(1, 'day')]
}

/**
 * 获取下周起止日期
 */
export function getNextWeekStartWithEnd(date) {
  const week = dayjs(date).add(1, 'week')
  return [week.startOf('week').add(1, 'day'), week.endOf('week').add(1, 'day')]
}

/**
 * 获取本月起止日期
 */
export function getThisMonthStartWithEnd(date) {
  const month = dayjs(date)
  return [month.startOf('month'), month.endOf('month')]
}

/**
 * 获取上月起止日期
 */
export function getLastMonthStartWithEnd(date) {
  const month = dayjs(date).subtract(1, 'month')
  return [month.startOf('month'), month.endOf('month')]
}

export function getSettleAccounts([start, end]) {
  return [dayjs(start).add(16, 'hour'), dayjs(end).add(16, 'hour')]
}

/**
 * 获取起止时间集合
 */
function getStartWithEndDateMap() {
  const todayStartWithEnd = getTodayStartWithEnd()
  const yesterdayStartWithEnd = getYesterdayStartWithEnd()
  const tomorrowStartWithEnd = getTomorrowStartWithEnd()
  const thisWeekStartWithEnd = getThisWeekStartWithEnd()
  const thisMonthStartWithEnd = getThisMonthStartWithEnd()
  const lastMonthStartWithEnd = getLastMonthStartWithEnd()
  return {
    昨日: yesterdayStartWithEnd,
    今日: todayStartWithEnd,
    明日: tomorrowStartWithEnd,
    本周: thisWeekStartWithEnd,
    本月: thisMonthStartWithEnd,
    同比: lastMonthStartWithEnd,
  }
}

export const startWithEndMap = getStartWithEndDateMap()

export function formatStartWithEnd(e) {
  if (e) {
    const start = dayjs(e[0]).startOf('day').format('YYYY-MM-DD HH:mm:ss')
    const end = dayjs(e[1]).endOf('day').format('YYYY-MM-DD HH:mm:ss')
    return {
      time: [start, end],
      start,
      end,
    }
  }
  return {
    time: [],
    start: undefined,
    end: undefined,
  }
}
